package com.lishe.lishe.retrofit.presenter;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseBean;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.myorder.activity.ConfirmOrderActivity;
import com.lishe.lishe.lishe.module.shopingcart.bean.SubmitOrderBean;
import com.lishe.lishe.lishe.utils.ImeiUtil;
import com.lishe.lishe.lishe.utils.NetWorkUtils;
import com.lishe.lishe.lishe.utils.ToastUtil;
import com.lishe.lishe.net.BaseHttpLoader;
import com.lishe.lishe.net.BaseProtocal;
import com.lishe.lishe.net.NetAPI;
import com.lishe.lishe.net.ResultListener;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.lishe.lishe.retrofit.bean.CreatePreOrderBean;
import com.lishe.lishe.retrofit.bean.PayOrderQueryObject;
import com.lishe.lishe.retrofit.interfaces.PayService;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * Created by DELL on 2017/11/22.
 */

public class PayPresenter {
    private static PayPresenter payPresenter;

    private PayPresenter() {

    }

    public static PayPresenter getInstance() {
        if (payPresenter == null) {
            payPresenter = new PayPresenter();
        }
        return payPresenter;
    }

    /**
     * 微信支付预订单
     *
     * @return
     */
    public void createPreOrder(double money, String type, final DataResponseListener<CreatePreOrderBean> listener) {
        RetrofitFactory.create().create(PayService.class)
                .createPreOrder(Globals.getToken(), money, type, ImeiUtil.getUniquePsuedoID())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                })
                .subscribe(new Observer<BaseRequestBody<CreatePreOrderBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<CreatePreOrderBean> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
